from haystack import indexes
from crouse.models import CrouseDeskModel

class CourseIndex(indexes.SearchIndex, indexes.Indexable):
    text = indexes.CharField(document=True, use_template=False)
    kname = indexes.CharField(model_attr="kname")
    intro = indexes.CharField(model_attr="intro")
    teacher = indexes.CharField(model_attr="teacher")
    id = indexes.IntegerField(model_attr="id")
    cimg = indexes.CharField(model_attr="cimg")
    price = indexes.DecimalField(model_attr="price")
    disc = indexes.DecimalField(model_attr="disc")

    # 指定与当前es索引模型对接的mysql的ORM模型
    def get_model(self):
        return CrouseDeskModel

    # 当用户搜索es索引时，对应的提供的mysql数据集有哪些？
    def index_queryset(self, using=None):
        return self.get_model().objects.all()
    def prepare_text(self,obj):
        return f"{obj.kname}{obj.intro}"